{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Transpose Matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: transpose"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #转置矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个二维整数数组 <code>matrix</code>， 返回 <code>matrix</code> 的 <strong>转置矩阵</strong> 。</p>\n",
    "\n",
    "<p>矩阵的 <strong>转置</strong> 是指将矩阵的主对角线翻转，交换矩阵的行索引与列索引。</p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/10/hint_transpose.png\" style=\"width: 600px; height: 197px;\" /></p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "<strong>输出：</strong>[[1,4,7],[2,5,8],[3,6,9]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>matrix = [[1,2,3],[4,5,6]]\n",
    "<strong>输出：</strong>[[1,4],[2,5],[3,6]]\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == matrix.length</code></li>\n",
    "\t<li><code>n == matrix[i].length</code></li>\n",
    "\t<li><code>1 <= m, n <= 1000</code></li>\n",
    "\t<li><code>1 <= m * n <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [transpose-matrix](https://leetcode.cn/problems/transpose-matrix/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [transpose-matrix](https://leetcode.cn/problems/transpose-matrix/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2,3],[4,5,6],[7,8,9]]', '[[1,2,3],[4,5,6]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "        transposed = [[0] * m for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                transposed[j][i] = matrix[i][j]\n",
    "        return transposed\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        l=[]\n",
    "        for i in range(n):\n",
    "            t = [0]*m\n",
    "            l.append(t)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                l[j][i]=matrix[i][j]\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        ans = list()\n",
    "        for j in range(len(matrix[0])):\n",
    "            row = list()\n",
    "            for i in range(len(matrix)):\n",
    "                row.append(matrix[i][j])\n",
    "            ans.append(row)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        a=[]\n",
    "        for i in range(len(matrix[0])):\n",
    "            temp=[]\n",
    "            for j in range(len(matrix)):\n",
    "                temp.append(matrix[j][i])\n",
    "            a.append(temp)\n",
    "        print(a)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        r = len(A)\n",
    "        c = len(A[0])\n",
    "        thr = max(r,c)\n",
    "        result = [[0] * thr for _ in range(thr)]\n",
    "        for i in range(r):\n",
    "            for j in range(0, c):\n",
    "                if i < r and j < c:\n",
    "                    result[j][i] = A[i][j]\n",
    "        for index_i in range(c):\n",
    "                result[index_i] = result[index_i][:r]\n",
    "        return result[:c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:# time O(N^2)\n",
    "        if len(A) == 0:\n",
    "            return []\n",
    "        \n",
    "        hig = len(A)\n",
    "        wid = len(A[0])\n",
    "        \n",
    "        ma = max(hig, wid)\n",
    "        \n",
    "        template = [[0]*ma for i in range(ma)]\n",
    "        \n",
    "        for i in range(hig):\n",
    "            for j in range(wid):\n",
    "                template[i][j] = A[i][j]\n",
    "        \n",
    "        for i in range(ma):\n",
    "            for j in range(i):\n",
    "                template[i][j], template[j][i] = template[j][i], template[i][j]\n",
    "        \n",
    "        final = [[0]*hig for i in range(wid)]\n",
    "        \n",
    "        for i in range(wid):\n",
    "            for j in range(hig):\n",
    "                final[i][j] = template[i][j]\n",
    "        \n",
    "        return final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "\n",
    "        dim1, dim2 = len(A), len(A[0])\n",
    "\n",
    "\n",
    "        if dim1 == dim2:\n",
    "\n",
    "            for i in range(dim1):\n",
    "                for j in range(i):\n",
    "                    A[i][j], A[j][i] = A[j][i], A[i][j]\n",
    "\n",
    "        elif dim1 > dim2:\n",
    "            for _ in A:\n",
    "                for i in range(dim1 - dim2):\n",
    "                    _.append(0)\n",
    "\n",
    "            for i in range(dim1):\n",
    "                for j in range(i):\n",
    "                    A[i][j], A[j][i] = A[j][i], A[i][j]\n",
    "\n",
    "            A[:] = [A[_] for _ in range(dim2)]\n",
    "\n",
    "        else:\n",
    "            for _ in range(dim2 - dim1):\n",
    "                A.append([0]*dim2)\n",
    "\n",
    "            for i in range(dim2):\n",
    "                for j in range(i):\n",
    "                    A[i][j], A[j][i] = A[j][i], A[i][j]\n",
    "\n",
    "            A[:] = [_[:dim1] for _ in A]\n",
    "\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "\n",
    "        if m == 1 or n == 1:\n",
    "            if m == 1:\n",
    "                return [[x] for x in matrix[0]]\n",
    "            else:\n",
    "                for i in range(1, len(matrix)):\n",
    "                    matrix[0].append(matrix[i][0])\n",
    "                matrix = [matrix[0]]\n",
    "                return matrix\n",
    "\n",
    "        if m != n:\n",
    "            if m > n:\n",
    "                for i in range(m):\n",
    "                    matrix[i].extend([0 for _ in range(m - n)])\n",
    "            else:\n",
    "                for _ in range(n - m):\n",
    "                    matrix.append([0 for _ in range(m)])\n",
    "\n",
    "        if n >= m:\n",
    "            for i in range(m):\n",
    "                for j in range(n):\n",
    "                    if i < j:\n",
    "                        matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n",
    "        else:\n",
    "            for i in range(m):\n",
    "                for j in range(n):\n",
    "                    if i > j:\n",
    "                        matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n",
    "        \n",
    "        if m != n:\n",
    "            if m > n:\n",
    "                for _ in range(m - n):\n",
    "                    matrix.pop()\n",
    "            else:\n",
    "                for i in range(m):\n",
    "                    matrix[i] = matrix[i][:m]\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        if m == n:\n",
    "            return self.transpose4(matrix)\n",
    "        if m > n:\n",
    "            t = [0]*(m-n)\n",
    "            for i in range(m):\n",
    "                matrix[i].extend(t)\n",
    "            return self.transpose4(matrix)[:n]\n",
    "        else:\n",
    "            c = n - m\n",
    "            a = [[0] * n for _ in range(c)]\n",
    "            matrix.extend(a)\n",
    "\n",
    "            matrix = self.transpose4(matrix)\n",
    "            for i in range(n):\n",
    "                matrix[i] = matrix[i][:m]\n",
    "            return matrix\n",
    "\n",
    "\n",
    "\n",
    "    def transpose4(self, t: List[List[int]]) -> List[List[int]]:\n",
    "        m = len(t)\n",
    "        n = len(t[0])\n",
    "        for i in range(m):\n",
    "            for j in range(i):\n",
    "                t[i][j], t[j][i] = t[j][i], t[i][j]\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "\n",
    "        n_col = len(A)\n",
    "        if not n_col:\n",
    "            return A\n",
    "        n_row = len(A[0])\n",
    "        if not n_row:\n",
    "            return A\n",
    "        \n",
    "        if n_col > n_row:\n",
    "            max_len = n_col\n",
    "            col_long = True\n",
    "        else:\n",
    "            max_len = n_row\n",
    "            col_long = False\n",
    "\n",
    "        if col_long:\n",
    "            for i in range(max_len):\n",
    "                A[i].extend([0] * (max_len - n_row))\n",
    "        else:\n",
    "            A.extend([[0]*max_len for _ in range(max_len - n_col)])\n",
    "\n",
    "        for i in range(max_len):\n",
    "            for j in range(i, max_len):\n",
    "                A[i][j], A[j][i] = A[j][i], A[i][j]\n",
    "        \n",
    "        if col_long:\n",
    "            return A[:n_row]\n",
    "        else:\n",
    "            return [row[:n_col] for row in A]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        n, m = len(matrix), len(matrix[0])\n",
    "        if n < m:\n",
    "            for _ in range(m-n):\n",
    "                matrix.append([0] * m)\n",
    "        elif m < n:\n",
    "            for i in range(n):\n",
    "                for _ in range(n-m):\n",
    "                    matrix[i].append(0)        \n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(i, len(matrix[0])):\n",
    "                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n",
    "\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            while matrix[i] and matrix[i][-1] == 0:\n",
    "                matrix[i].pop()\n",
    "        \n",
    "        index = 0\n",
    "        while index < len(matrix):\n",
    "            if not matrix[index]:\n",
    "                matrix.pop(index)\n",
    "            else:\n",
    "                index += 1\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        a = len(matrix)\n",
    "        b = len(matrix[0])\n",
    "        temp = []\n",
    "        if a<b:\n",
    "            for j in range(b):\n",
    "                temp.append(0)\n",
    "\n",
    "            for i in range(a,b):                \n",
    "                matrix.append(copy.copy(temp))\n",
    "            \n",
    "            for m in range(len(matrix)):\n",
    "                for n in range(m,len(matrix[0])):\n",
    "                    x = 0\n",
    "                    x = matrix[m][n]\n",
    "                    matrix[m][n] = matrix[n][m]\n",
    "                    matrix[n][m] = x\n",
    "\n",
    "            matrix = [tmp[0:a] for tmp in matrix]\n",
    "            return matrix\n",
    "\n",
    "        if a>b:\n",
    "            for i in range(a):\n",
    "                for j in range(b,a):\n",
    "                    matrix[i].append(0)\n",
    "\n",
    "            for m in range(len(matrix)):\n",
    "                for n in range(m,len(matrix[0])):\n",
    "                    matrix[m][n],matrix[n][m] = matrix[n][m],matrix[m][n]\n",
    "\n",
    "            return matrix[0:b]\n",
    "\n",
    "        if a==b:\n",
    "            for m in range(len(matrix)):\n",
    "                for n in range(m,len(matrix[0])):\n",
    "                    matrix[m][n],matrix[n][m] = matrix[n][m],matrix[m][n]\n",
    "            \n",
    "            return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        rows, cols = len(matrix), len(matrix[0])\n",
    "        if rows < cols:\n",
    "            for i in range(cols - rows):\n",
    "                matrix.append([0 for _ in range(cols)])\n",
    "            print(matrix)\n",
    "        elif rows > cols:\n",
    "            for i in range(rows):\n",
    "                for j in range(rows - cols):\n",
    "                    matrix[i].append(0)\n",
    "        lines = max(rows, cols)\n",
    "        for i in range(lines):\n",
    "            for j in range(i+1, lines):\n",
    "                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n",
    "        if rows < cols:\n",
    "            for i in range(cols):\n",
    "                matrix[i] = matrix[i][:rows]\n",
    "        if rows > cols:\n",
    "            matrix = matrix[:cols]\n",
    "\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        height = len(A)\n",
    "        width = len(A[0])\n",
    "        if height > width:\n",
    "            for i in range(height - width):\n",
    "                for j in range(height):\n",
    "                    A[j].append(0)\n",
    "        else:\n",
    "            L = []\n",
    "            for i in range(width - height):\n",
    "                for j in range(width):\n",
    "                    L.append(0)\n",
    "                A.append(L)\n",
    "                L = []\n",
    "        for i in range(len(A)-1):\n",
    "            for j in range(i+1):\n",
    "                swap = A[i+1][j]\n",
    "                A[i+1][j] = A[j][i+1]\n",
    "                A[j][i+1] = swap\n",
    "\n",
    "        A = A[0 : width]\n",
    "        for i in range(width):\n",
    "            A[i] = A[i][0:height]\n",
    "        return A\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "      m = len(matrix)\n",
    "      n = len(matrix[0])\n",
    "      gap = abs(n - m)\n",
    "      if m < n:\n",
    "          for i in range(gap):\n",
    "              matrix.append([0 for k in range(n)])\n",
    "      elif m > n:\n",
    "          for i in range(m):\n",
    "              for k in range(gap):\n",
    "                  matrix[i].append(0)\n",
    "      for i in range(len(matrix)):\n",
    "          for j in range(len(matrix[i])):\n",
    "              if i < j:\n",
    "                  temp = matrix[i][j]\n",
    "                  matrix[i][j] = matrix[j][i]\n",
    "                  matrix[j][i] = temp\n",
    "      if m < n:\n",
    "          newm = len(matrix)\n",
    "          newn = len(matrix[0])\n",
    "          for i in range(newm):\n",
    "              for k in range(gap):\n",
    "                  matrix[i].pop()\n",
    "      elif m > n:\n",
    "          newm = len(matrix)\n",
    "          newn = len(matrix[0])\n",
    "          for i in range(gap):\n",
    "              matrix.pop()\n",
    "      return matrix\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np \n",
    "        return np.transpose(A).tolist()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        A=np.transpose(A)\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        arr=np.array(A)\n",
    "        return arr.T        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        return np.array(A).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        temp=[]\n",
    "        temp += list(zip(*A))\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        return np.array(A).T\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        return zip(*A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "                \n",
    "\n",
    "        rows = len(A)\n",
    "        colunms = len(A[0])\n",
    "\n",
    "        new_li = [[0 for _ in range(rows)] for _ in range(colunms)]\n",
    "\n",
    "        for i in range(colunms):\n",
    "            for j in range(rows):\n",
    "                new_li[i][j] = A[j][i]\n",
    "        return new_li"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np \n",
    "        A = np.array(A)\n",
    "        ans = [A[:,i] for i in range(A.shape[1])]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np \n",
    "        a = np.array(A)\n",
    "        return a.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        m=len(A)\n",
    "        n=len(A[0])\n",
    "        B=np.zeros((n,m),dtype= np.int)\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                B[i][j]=int(A[j][i])\n",
    "        return B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        m = len(A)\n",
    "        n = len(A[0])\n",
    "        B = np.zeros(shape=(n,m),dtype=int)\n",
    "        for i in range(m):\n",
    "            for j in range(n) :\n",
    "                B[j][i] = A[i][j]\n",
    "        return B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        R, C = len(A), len(A[0])\n",
    "        ans = [[None] * R for _ in range(C)]\n",
    "        for r, row in enumerate(A):\n",
    "            for c, val in enumerate(row):\n",
    "                ans[c][r] = val\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, A: List[List[int]]) -> List[List[int]]:\n",
    "        return list(np.array(A).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        res = [[1]*len(matrix) for _ in range(len(matrix[0]))]\n",
    "        for i in range(len(res)):\n",
    "            for j in range(len(res[0])):\n",
    "                res[i][j] = matrix[j][i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        res = np.empty((len(matrix[0]),len(matrix)))\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                res[j][i] = matrix[i][j]\n",
    "        return [list(map(int,row)) for row in res.tolist()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        # m,n = len(matrix),len(matrix[0])\n",
    "        # # 这里m和n不一定相等 不能原地修改\n",
    "        # res = [[0]*m for _ in range(n)]\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         res[j][i] = matrix[i][j]\n",
    "\n",
    "        # return res\n",
    "\n",
    "        import numpy as np\n",
    "        # 老规矩了 list转成numpy.array 然后调用转置 将类型转回list\n",
    "        return np.array(matrix).T.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        \"\"\"\n",
    "        return np.transpose(matrix).tolist()\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        col_len = len(matrix)\n",
    "        row_len = len(matrix[0])\n",
    "\n",
    "        for i in range(row_len):\n",
    "            row = []\n",
    "            for j in range(col_len):\n",
    "                row.append(matrix[j][i])\n",
    "            res.append(row)\n",
    "\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        \"\"\"\n",
    "        return np.transpose(matrix).tolist()\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        col_len = len(matrix)\n",
    "        row_len = len(matrix[0])\n",
    "\n",
    "        for i in range(row_len):\n",
    "            row = []\n",
    "            for j in range(col_len):\n",
    "                row.append(matrix[j][i])\n",
    "            res.append(row)\n",
    "\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        return np.transpose(matrix).tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np;\n",
    "        return np.transpose(matrix).tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        import numpy as np\n",
    "        return (np.array(matrix)).swapaxes(0,1).tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        # m = len(matrix)\n",
    "        # if m == 0:\n",
    "        #     return matrix\n",
    "        # n = len(matrix[0])\n",
    "        # # for row in mat:\n",
    "        # #     if len(row) != n:\n",
    "        # #         return matrix\n",
    "        \n",
    "        # # new_mat = np.zeros((n, m), dtype=int)\n",
    "        # # for i in range(m):\n",
    "        # #     for j in range(n):\n",
    "        # #         new_mat[j][i] = matrix[i][j]\n",
    "        # # return new_mat.tolist()\n",
    "\n",
    "        # new_mat = []\n",
    "        # for i in range(n):\n",
    "        #     new_row = []\n",
    "        #     for j in range(m):\n",
    "        #         new_row.append(matrix[j][i])\n",
    "        #     new_mat.append(new_row)\n",
    "        # return new_mat\n",
    "\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "        transposed = [[0] * m for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                transposed[j][i] = matrix[i][j]\n",
    "        return transposed\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        return np.array(matrix).T.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n",
    "        return np.array(matrix).T.tolist()"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
